﻿var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext="<p align=center><img src='/js/loading.gif' /><br>Đang tải dữ liệu</p>"
var menutag="td"
////NO NEED TO EDIT BELOW////////////////////////
var loadedobjects=""
var defaultcontentarray=new Object()
var cacheArray=new Object()
var bustcacheparameter=""

function savecache(contentid){
    if (typeof cacheArray[contentid]=="undefined") //if default content hasn't already been saved
	cacheArray[contentid]=document.getElementById(contentid).innerHTML
}

function loadHTML(sourceid, containerid, targetobj){
	var ullist=targetobj.parentNode.parentNode.getElementsByTagName(menutag);
	for (var i=0; i<ullist.length; i++){
	    var link=ullist[i].getElementsByTagName("a")[0];
	    if(ullist[i].className=="selected" && typeof(link)!="undefined"){
	        savecache(getHref(link));
	    }
	    //deselect all tabs
		if(i<ullist.length-1)
		    ullist[i].className="";
		else
			ullist[i].className="last";
	}  
	targetobj.parentNode.className="selected";
	
//	if (url.indexOf("#default")!=-1){
//		document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
//		return
//	}

    
	document.getElementById(containerid).innerHTML=loadstatustext
	if (typeof cacheArray[sourceid]=="undefined"){
	    document.getElementById(containerid).innerHTML=document.getElementById(sourceid).innerHTML;
	    savecache(sourceid);
	    document.getElementById(sourceid).innerHTML='';
	}
	else{
	    document.getElementById(containerid).innerHTML=cacheArray[sourceid];
	}
}

function loadobjs(revattribute){
	if (revattribute!=null && revattribute!=""){ //if "rev" attribute is defined (load external .js or .css files)
		var objectlist=revattribute.split(/\s*,\s*/) //split the files and store as array
		for (var i=0; i<objectlist.length; i++){
			var file=objectlist[i]
			var fileref=""
				if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
				if (file.indexOf(".js")!=-1){ //If object is a js file
					fileref=document.createElement('script')
					fileref.setAttribute("type","text/javascript");
					fileref.setAttribute("src", file);
				}
				else if (file.indexOf(".css")!=-1){ //If object is a css file
					fileref=document.createElement("link")
					fileref.setAttribute("rel", "stylesheet");
					fileref.setAttribute("type", "text/css");
					fileref.setAttribute("href", file);
				}
			}
			if (fileref!=""){
				document.getElementsByTagName("head").item(0).appendChild(fileref)
				loadedobjects+=file+" " //Remember this object as being already added to page
			}
		}
	}
}

function savedefaultcontent(contentid){// save default ajax tab content
	if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
	defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
}

function savedefaultcontent(contentid){// save default ajax tab content
	if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
	defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
}

function getHref(link){
    var url = link.getAttribute("href");
    return (url.indexOf("#")> -1) ? url.substring(url.lastIndexOf("#")+1,url.length) : "#"+url;
}
function getRel(link){
    return link.getAttribute("rel");
}
function getRev(link){
    return link.getAttribute("rev");
}

function startjtabs(){
    var c_url = location.href;
    var c_id = (c_url.lastIndexOf("#")>0) ? c_url.substring(c_url.lastIndexOf("#")+1,c_url.length) : null;
	for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
		var ulobj=document.getElementById(arguments[i])
		var ulist=ulobj.getElementsByTagName(menutag) //array containing the LI elements within UL
		for (var x=0; x<ulist.length; x++){ //loop through each LI element
			var ulistlink=ulist[x].getElementsByTagName("a")[0]
			if (getRel(ulistlink)){
				var modifiedurl0='#'+getHref(ulistlink);
				
				var modifiedurl = modifiedurl0.replace('##','');
				
				ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
				
				if(!modifiedurl0.indexOf('##') > -1){
				    savedefaultcontent(getRel(ulistlink)) //save default ajax tab content
    				
				    ulistlink.onclick=function(){
					    loadHTML(getHref(this),getRel(this), this)
					    loadobjs(getRev(this))
					    return false
				    }
    				
				    if (ulist[x].className=="selected"){
					    loadHTML(getHref(ulistlink),getRel(ulistlink), ulistlink) //auto load currenly selected tab content
					    loadobjs(getRev(ulistlink)) //auto load any accompanying .js and .css files
				    }
    				
				    if(getHref(ulistlink)==c_id){
				        loadHTML(getHref(ulistlink),getRel(ulistlink), ulistlink)
					    loadobjs(getRev(ulistlink));
				    }
				}    
			}
		}
	}
}